<?php

namespace App\Service\Work;

use App\Models\Works\CommentModel;

use DB;

class CommentService
{
    public static function addComments(array $data)
    {
        $data['created_at'] = date("Y-m-d H:i:s");
        if(CommentModel::insert($data)){
            return true;
        }
        return false;
    }

    /**
     * @param $articleid
     * @param $page
     * @return mixed
     */
    public static function getCommentsList($articleid,$page)
    {

        $comments = CommentModel::join('lsj_user','lsj_user.uid','=','lsj_comment_log.uid')
            ->where(['worksid' => $articleid,'rid' => 0])
            ->skip($page)
            ->take(10)
            ->select('lsj_comment_log.*','lsj_user.nickname','lsj_user.uid','lsj_user.headimgurl')
            ->orderBy('lsj_comment_log.created_at','desc')
            ->get()
            ->toArray();

        foreach($comments as $key=>$info){
            $rcomments  =  CommentModel::join('lsj_user','lsj_user.uid','=','lsj_comment_log.uid')
                ->where(['worksid'=> $articleid,'rid'=>$info['id']])
                ->orderBy('lsj_comment_log.created_at','desc')
                ->select('lsj_comment_log.*','lsj_user.nickname','lsj_user.uid','lsj_user.headimgurl')
                ->take(2)
                ->get();
            $rcomments_num = CommentModel::where(['worksid'=> $articleid,'rid'=>$info['id']])->count();
            $comments[$key]['responce'] = $rcomments->toArray();
            $comments[$key]['responce_num'] = $rcomments_num;
        }
            return $comments;
    }

    /**
     * 获得回复列表
     */
    public static function getCommentResponce($rid, $page)
    {
        $result = CommentModel::join('lsj_user','lsj_user.uid','=','lsj_comment_log.uid')
            ->where(['rid' => $rid])
            ->skip($page)
            ->take(10)
            ->select('lsj_comment_log.*','lsj_user.nickname','lsj_user.uid','lsj_user.headimgurl')
            ->orderBy('lsj_comment_log.created_at','desc')
            ->get()
            ->toArray();
        return $result;
    }
	
	 /**
     * 获得回复评论
     */
    public static function getComments($id)
    {
        $result = CommentModel::where("id",$id)->first();
        return $result;
    }
}
        